<?php
/* 
程序作者:medias 冷迪中文网www.lengdi.com
*/
//得到现在时间
$timestamp = time();
$flush="864000";
$flush1="864000";

include 'config.php';
function open($file,$type=''){
        global $fromurl,$referer;
        $cachename=$file;
                if($type){
                        $file=$fromurl.'/'.$type.'/'.$file;
                }else{
                        $file=$fromurl.$file;
                }
                        if($open=file($file)){
                                        $count=count($open);
                                        for($i=0;$i<$count;$i++){
                                                $theget.=$open[$i];
                                        }
                                }else{
                                        die('请求过多，超时，请刷新');
                                }
        return $theget;
}

function cut($file,$from,$end){
        $message=explode($from,$file);
        $message=explode($end,$message[1]);
return        $message[0];
}

function   path($path)  
  {  
  if(!@dir($path)){  
  mkdir($path,0777);  
  }  
  }   

function curl($openurl)
{
	global $isfun;
	if($isfun=="curl"){
	$ch = curl_init($openurl);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$file = curl_exec($ch);
	curl_close($ch);
	}
	elseif($isfun=="fopen"){
		$file=open($openurl);
	}
	else{ echo "<script>alert('你的空间不支持本程序，谢谢使用');window.close();</script>";}
	return $file;
}

function mtime($cachename){
//判断文件修改时间
		if(file_exists($cachename)){
		$lastflesh=@filemtime($cachename);
		return $lastflesh;
		}
}


Function cachefile1($cachename)
{ global $timestamp,$flush;
//判断文件是否存在开始
if(!file_exists($cachename)){
$mystr="";
}else{
//判读文件时间开始
$lastflesh=@filemtime($cachename);
if($lastflesh + ($flush * 60) < $timestamp ){
$mystr="";	
}
else{
//读取文件开始
//if($open=file($cachename))
//$fp2 = @fopen($cachefilename,"w")
$mystr	= "111";
		}
//读取文件结束

//判读文件时间结束
}
//判断文件是否存在结束
return $mystr;
}

Function readtpl($cachename)
{ global $timestamp,$flush;
//判断文件是否存在开始
if(!file_exists($cachename)){
$mystr="";
}
else{
//读取文件开始
if($open=file($cachename))
$fp2 = @fopen($cachename,"r");
@flock($fp2, 2);
$fup = fread($fp2,filesize($cachename)); 
fclose($fp2);
$mystr	= $fup;
		}
//读取文件结束

//判读文件时间结束

//判断文件是否存在结束
return $mystr;
}


Function cachexml($cachename)
{ global $timestamp,$flush1;
//判断文件是否存在开始
if(!file_exists($cachename)){
$mystr="";
}else{
//判读文件时间开始
$lastflesh=@filemtime($cachename);
if($lastflesh + ($flush1 * 60) < $timestamp ){
$mystr="";	
}else{
//读取文件开始
//if($open=file($cachename))
//$fp2 = @fopen($cachefilename,"w")
$xml= new SimplexmlElement($cachename,NULL,TRUE);}
if (!$xml) {
	$mystr="";
}
else{
	$mystr="1";
//读取文件结束
}
//判读文件时间结束
}
//判断文件是否存在结束
return $mystr;
}


function getbook($bid){
//获取信息，并获取列表
global $DB;
$bbid	=	floor($bid/1000);
$openurl	=	"http://www.101du.net/files/article/html/".$bbid."/".$bid."/index.html";
$openurl1	=	"http://www.101du.net/files/article/info/".$bbid."/".$bid.".htm";
$file1	=	curl($openurl1);
$file	=	curl($openurl);
$file 	=	iconv("GBK", "UTF-8//IGNORE", $file);
$file1 	=	iconv("GBK", "UTF-8//IGNORE", $file1);
$type	=	cut($file1,"类&nbsp;&nbsp;&nbsp; 别：","</td>");
//类别
$clong	=	cut($file1,"全文长度：","字</td>");
//全文长度
$allhit	=	cut($file1,"总点击数：","</td>");
//总点击数
$monthhit	=	cut($file1,"本月点击：","</td>");
//总点击数
$weekhit	=	cut($file1,"本周点击：","</td>");
//总点击数
$hottext	=	cut($file1,"内容简介：</span><br />","<br /><br />");
//简介
//<img src=\"http://www.101du.net/files/article/image/5/5094/5094s.jpg\" border=\"0\" width=\"100\" height=\"125\" align=\"right\" hspace=\"5\" vspace=\"5\" />

$pic		=	cut($file1,"<td width=\"80%\" valign=\"top\">","\" border=\"0\" width=\"100\" height=\"125\" align=\"right\" hspace=\"5");
$pic		=	cut($pic,"<img src=\"","\"");
echo "<img src=".$pic." />";
//echo $type."\n".$clong."\n".$allhit."\n".$monthhit."\n".$weekhit."\n".$hottext;
//$title	=	cut($file,"<div id=\"title\">","</div>");
//$title	=	str_replace("<h1>","",$title);

$author	=	cut($file,"<div id=\"info\">","</div>");
$rule		=	"|<a href=\"([0-9]+).html\">(.+?)</a>|U";
preg_match_all($rule,$file,$matchs);
$count = count($matchs[0]);
$cachefilename	=	ROOT_PATH."/cached/b/".$bbid."/".$bid.".xml";
$xxxml			=	ROOT_PATH."/xml.xml";
$xml = new SimplexmlElement($xxxml,NULL,TRUE);
$books	=	$xml->addChild('books');
//echo $count;
	for($i=0;$i<$count;$i++){
	$charts	=	$xml->books->addChild('charts');
	$charts->addChild('id',xmlencode($matchs[1][$i]));
	$charts->addChild('title',xmlencode($matchs[2][$i]));
	$lastname	=	$matchs[2][$i];
	}
	echo $lastname;
	$xml->asXML($cachefilename);
	$author	=	str_replace("作者：","",$author);
	$sql	=	"UPDATE `book_list` SET `updatetime` = NOW(),`lastname` = '$lastname',`author` = '$author',`type` = '$type',`clong` = '$clong',`allhit` = '$allhit',`monthhit` = '$monthhit',`weekhit` = '$weekhit',`hottext` = '$hottext'	 WHERE `bid` = '$bid'"; 
	$query	=	$DB->query($sql);	
		$picfile	=	curl($pic);
		if ($picfile == ""){
		$fp2 = @fopen("none.jpg","r");
		@flock($fp2,2);
		@flock($fp2,2);
		$picfile=@fread($fp2,filesize("none.jpg"));
		@fclose($fp2);
		}
		$fp2 = @fopen(ROOT_PATH."/pic/".$bbid."/".$bid.".jpg","w");
		@flock($fp2,2);
		@fwrite($fp2,$picfile);
		@fclose($fp2);
	echo "更新".$bid."<br /> \n";
return TRUE;}

function dobook($bid,$cid){
		$xxxml			=	ROOT_PATH."/xml.xml";
		$bbid	=	floor($bid/1000);
		$cachexmlname	=	ROOT_PATH."/cached/c/xml/".$bbid."/".$bid."/".$cid.".xml";
		$openurl	=	"http://www.101du.net/files/article/html/".$bbid."/".$bid."/".$cid.".html";
		$file	=	curl($openurl);
		$file 	=	iconv("GBK", "UTF-8//IGNORE", $file);
		$title	=	cut($file,"<div id=\"title\">","</div>");
		$preview_page	=	cut($file,"preview_page = \"",".html");
		$nextpage	=	cut($file,"next_page = \"",".html");
		$content	=	cut($file,"<div id=\"content\">","<div class=\"jubao\">");
		$content	=	str_replace("</div>","",$content);
		$content	=	str_replace("<br /><br />","<br />",$content);
		$content	=	str_replace("&nbsp;","",$content);
		//echo $title."<br /> \n";
			if($preview_page == "index"){
				$preview_page_c		=	"book.php?bid=".$bid;
				$pcid				=	"";
			}
			else{
				$preview_page_c	=	"reader.php?bid=".$bid."&cid=".$preview_page;
				$pcid			=	$preview_page;
			}
			if($nextpage == "index"){
				$nextpage_c		=	"book.php?bid=".$bid;
				$ncid				=	"";
			}
			else{
				$nextpage_c		=	"reader.php?bid=".$bid."&cid=".$nextpage;
				$ncid				=	$nextpage;
			}
//缓存不存在，远程采集子程序结束
//XML处理程序开始
		$xml = new SimplexmlElement($xxxml,NULL,TRUE);
		$books	=	$xml->addChild('books');
		$books->addChild('title',xmlencode($title));
		$books->addChild('bid',xmlencode($bid));
		$books->addChild('pcid',xmlencode($pcid));
		$books->addChild('ncid',xmlencode($ncid));
		$books->addChild('content',xmlencode($content));
		$xml->asXML($cachexmlname);
}
function xmlencode($tag)
{
$tag = str_replace("&", "&amp;", $tag);
$tag = str_replace("<", "&lt;", $tag);
$tag = str_replace(">", "&gt;", $tag);
$tag = str_replace("'", "&apos;", $tag);
$tag = str_replace("\"", '&quot;', $tag);
return $tag;
}

function getbook_info($bid){
//获取信息
global $DB;
$bbid	=	floor($bid/1000);
$openurl1	=	"http://www.101du.net/files/article/info/".$bbid."/".$bid.".htm";
$file1	=	curl($openurl1);
$file1 	=	iconv("GBK", "UTF-8//IGNORE", $file1);
$type	=	cut($file1,"类&nbsp;&nbsp;&nbsp; 别：","</td>");
//类别
$clong	=	cut($file1,"全文长度：","字</td>");
//全文长度
$allhit	=	cut($file1,"总点击数：","</td>");
//总点击数
$monthhit	=	cut($file1,"本月点击：","</td>");
//总点击数
$weekhit	=	cut($file1,"本周点击：","</td>");
//总点击数
$hottext	=	cut($file1,"内容简介：</span><br />","<br /><br />");
//简介
//图片
$pic		=	cut($file1,"<td width=\"80%\" valign=\"top\">","\" border=\"0\" width=\"100\" height=\"125\" align=\"right\" hspace=\"5");
$pic		=	cut($pic,"<img src=\"","\"");
echo "<img src=".$pic." />";
//作者
//$author	=	cut($file,"<div id=\"info\">","</div>");
$author	=	cut($file1,"作&nbsp;&nbsp;&nbsp; 者：","</td>");
$sql	=	"UPDATE `book_list` SET `updatetime` = NOW(),`author` = '$author',`type` = '$type',`clong` = '$clong',`allhit` = '$allhit',`monthhit` = '$monthhit',`weekhit` = '$weekhit',`hottext` = '$hottext'	 WHERE `bid` = '$bid'"; 
		$query	=	$DB->query($sql);	
		$picfile	=	curl($pic);
		if ($picfile == ""){
		$fp2 = @fopen("none.jpg","r");
		@flock($fp2,2);
		@flock($fp2,2);
		$picfile=@fread($fp2,filesize("none.jpg"));
		@fclose($fp2);
		}
		$fp2 = @fopen(ROOT_PATH."/pic/".$bbid."/".$bid.".jpg","w");
		@flock($fp2,2);
		@fwrite($fp2,$picfile);
		@fclose($fp2);
	echo "更新".$bid."<br /> \n";
return TRUE;}
 
function getlist($bid){
//获取列表
global $DB;
$bbid	=	floor($bid/1000);
$openurl	=	"http://www.101du.net/files/article/html/".$bbid."/".$bid."/index.html";
$file	=	curl($openurl);
$file 	=	iconv("GBK", "UTF-8//IGNORE", $file);
$rule		=	"|<a href=\"([0-9]+).html\">(.+?)</a>|U";
preg_match_all($rule,$file,$matchs);
$count = count($matchs[0]);
$cachefilename	=	ROOT_PATH."/cached/b/".$bbid."/".$bid.".xml";
$xxxml			=	ROOT_PATH."/xml.xml";
$xml = new SimplexmlElement($xxxml,NULL,TRUE);
$books	=	$xml->addChild('books');
//echo $count;
	for($i=0;$i<$count;$i++){
	$charts	=	$xml->books->addChild('charts');
	$charts->addChild('id',xmlencode($matchs[1][$i]));
	$charts->addChild('title',xmlencode($matchs[2][$i]));
	$name	=	$matchs[2][$i];
	}
	$xml->asXML($cachefilename);
	echo "更新".$bid."<br /> \n";
return $name;}
?>